package com.ternnetwork.core.service.impl.org;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.dao.org.OrganizationDao;
import com.ternnetwork.core.dao.org.OrganizationUserRelationshipDao;
import com.ternnetwork.core.entity.org.Organization;
import com.ternnetwork.core.service.org.OrganizationService;
import com.ternnetwork.core.ui.OrganizationZtree;

@Service("organizationService")
public class OrganizationServiceImpl implements OrganizationService{
	
	
	@Resource
	private OrganizationDao organizationDao;
	
	@Resource
	private OrganizationUserRelationshipDao organizationUserRelationshipDao;

	@Override
	public BaseResponse idoAdd(Organization t) {
        
		BaseResponse res=new BaseResponse();
        
    	if(StringUtils.isEmpty(t.getName())){
			res.setMessage("操作失败，中文名称不能为空!");
			res.setStatus(0);
			return res;
		}
		
		if(StringUtils.isEmpty(t.getCode())){
			res.setMessage("操作失败，代码不能为空!");
			res.setStatus(0);
			return res;
		}
		if(t.getSeqNum()==null){
			res.setMessage("操作失败，排序不能为空!");
			res.setStatus(0);
			return res;
		}
		long totalCount=organizationDao.getTotalCount("select count(id) from Organization where code=?1", t.getCode());
		if(totalCount>0L){
			res.setMessage("操作失败，代码已经存在!");
			res.setStatus(0);
			return res;
		}
		Organization pt=organizationDao.findById(Organization.class,t.getParentId());
		if(pt==null){
			res.setMessage("操作失败，父节点不存在!");
			res.setStatus(0);
		}
		organizationDao.persist(t);
		res.setMessage("操作成功!");
		res.setStatus(1);
		res.setData(t);
		return res;
	}

	@Override
	public BaseResponse idoUpdate(Organization t) {

		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getName())){
			res.setMessage("操作失败，中文名称不能为空!");
			res.setStatus(0);
			return res;
		}
		
		
		if(StringUtils.isEmpty(t.getCode())){
			res.setMessage("操作失败，代码不能为空!");
			res.setStatus(0);
			return res;
		}
		
		if(t.getSeqNum()==null){
			res.setMessage("操作失败，排序不能为空!");
			res.setStatus(0);
			return res;
		}
		
		Organization oldOrganization=organizationDao.findById(Organization.class,t.getId());
		if(oldOrganization==null){
			res.setMessage("操作失败，对象不存在!");
			res.setStatus(0);
			return res;
		}
		long totalCount=organizationDao.getTotalCount("select count(id) from Organization where code=?1 and id!=?2", t.getCode(),t.getId());
		
		if(totalCount>0L){
			res.setMessage("操作失败，代码已经存在!");
			res.setStatus(0);
			return res;
		}
		
		oldOrganization.setName(t.getName());
		oldOrganization.setSeqNum(t.getSeqNum());
		oldOrganization.setCode(t.getCode());
		oldOrganization.setAddress(t.getAddress());
		oldOrganization.setContactPerson(t.getContactPerson());
		oldOrganization.setEmail(t.getEmail());
		oldOrganization.setTelephone(t.getTelephone());
		oldOrganization.setParentId(t.getParentId());
		oldOrganization.setUpdateTime(new Date());
		organizationDao.saveOrUpdate(oldOrganization);
		res.setMessage("操作成功!");
		res.setStatus(1);
		res.setData(oldOrganization);
		return res;
	}

	@Override
	public BaseResponse idoDelete(long id) {
		BaseResponse res=new BaseResponse();
		Organization oldOrganization=organizationDao.findById(Organization.class,id);
		if(oldOrganization==null){
			res.setMessage("操作失败，对象不存在!");
			res.setStatus(0);
			return res;
		}
		organizationUserRelationshipDao.delete(id);
		organizationDao.delete(oldOrganization);
		res.setMessage("操作成功!");
		res.setStatus(1);
		return res;
	}

	@Override
	public List<OrganizationZtree> getZTreeJSON() {
		List<Organization> result=organizationDao.findAll("from Organization sm order by sm.seqNum");
		List<OrganizationZtree> treeList=new ArrayList<OrganizationZtree>();
		for(int i=0;i<result.size();i++){
			Organization t=(Organization)result.get(i);
			OrganizationZtree tree=new OrganizationZtree();
			tree.setId(t.getId());
			tree.setpId(t.getParentId());
			tree.setName(t.getName());
			tree.setSeqNum(t.getSeqNum());
			tree.setCode(t.getCode());
			tree.setAddress(t.getAddress());
			tree.setContactPerson(t.getContactPerson());
			tree.setEmail(t.getEmail());
			tree.setTelephone(t.getTelephone());
			tree.setParentId(t.getParentId());
			treeList.add(tree);
		}
		return treeList;
	}

}
